<script lang="ts">
	import { TUILogin } from '@tencentcloud/tui-core';
	import * as Push from '@/uni_modules/TencentCloud-Push';
	// #ifdef APP-PLUS || H5
	import { TUIChatKit } from './TUIKit';
	TUIChatKit.init();
	// #endif

	let vueVersion = 2;
	// #ifdef VUE3
	vueVersion = 3;
	// #endif

	//集成 TencentCloud-Push

	let info = require('@/siteinfo');
	console.log(info,'infoinfoinfo')
	var user_info = uni.getStorageSync('user_info') || {};
	uni.$SDKAppID = info.SDKAPPID; // Your SDKAppID
	uni.$userID = user_info.id; // Your userID
	uni.$userSig = uni.getStorageSync('userSig'); // Your userSig

	console.log(uni.$SDKAppID, "uni.$SDKAppID");
	console.log(uni.$userID, "uni.$userID");
	console.log(uni.$userSig, "uni.$userSig");

	uni.$TUICallKit = uni.requireNativePlugin('TencentCloud-TUICallKit');
	console.log(uni.$TUICallKit, "TUICallKit ｜ ok"); // 本地日志 
	export default {
		onLaunch: function (e) {
			if (uni.$SDKAppID && uni.$userID && uni.$userSig) {
				TUILogin.login({
					SDKAppID: uni.$SDKAppID,
					userID: uni.$userID,
					userSig: uni.$userSig,
					useUploadPlugin: true, // 已购买内容审核服务
					framework: `vue${vueVersion}` //当前开发使用框架
				}).catch((err) => {console.log(err,'我是错了的') });
				
				Push.setRegistrationID(uni.$userID, () => {
					console.log('setRegistrationID ok', uni.$userID);
				});
               //yXLimGwcsximmucVGwZqDmdkeOhx5edCRXomapjST95pjozKteY66iNBYs0tNsqN
			   //hje3AT1lNUcyRix6FALaYJYeXHNHjyuXf8snd5Kh5scowmZPk5kBZ8ltQbBGbWci
				Push.registerPush(uni.$SDKAppID, 'hje3AT1lNUcyRix6FALaYJYeXHNHjyuXf8snd5Kh5scowmZPk5kBZ8ltQbBGbWci', (
					data) => {
					// console.log('监听通知栏声音', data);
					Push.getRegistrationID((registrationID) => {
						console.log('getRegistrationID ok', registrationID);
					});
				}, (errCode, errMsg) => {
					console.error('这咋错了呢', errCode, errMsg);
				});
				Push.addPushListener(Push.EVENT.NOTIFICATION_CLICKED, (res) => {
					console.log('监听通知栏点击事件', res);
				});

				Push.addPushListener(Push.EVENT.MESSAGE_RECEIVED, (res) => {
					// console.log('监听在线推送', res);
				});

				Push.addPushListener(Push.EVENT.MESSAGE_REVOKED, (res) => {
					console.log('监听在线推送被撤回', res);
				});



			}








			// uni.hideTabBar();
			// console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！');
			console.log('App Launch', e);
			if (e.query.token) {
				uni.setStorageSync('ACCESS_TOKEN', e.query.token);
			}
			// #ifdef APP-PLUS
			// uni.setStorageSync('openid', 'ounkJ5CiIg9ap1nikiiGAO2ariQQ');
			// #endif

			uni.getSystemInfo({
				success: e => {
					this.globalData.statusBar = e.statusBarHeight; //状态栏高度
					let custom = {
						width: 0,
						height: 28,
						top: 0,
						left: 0,
						right: 0,
						bottom: 0
					}; //菜单按钮
					//#ifdef MP-WEIXIN || MP-ALIPAY
					custom = uni.getMenuButtonBoundingClientRect(); //菜单按钮
					console.log('custom', custom);
					if (custom.height == 0) {
						custom.height = 40;
						console.log('未获取到custom，手动设置为', custom);
					}
					//#endif

					if (e.safeArea) {
						// #ifdef MP-WEIXIN
						this.globalData.safeArea = e.screenHeight - e.safeArea.bottom
						// #endif
						// #ifndef MP-WEIXIN
						this.globalData.safeArea = e.safeAreaInsets.bottom
						// #endif
					} else {
						this.globalData.safeArea = 0
					}

					this.globalData.screenHeight = e.screenHeight;
					this.globalData.screenWidth = e.screenWidth;
					this.globalData.custom = custom;
					this.globalData.customBar = (custom.bottom + custom.top - e.statusBarHeight) > 0 ?
						(custom.bottom + custom.top - e.statusBarHeight) : (custom.height + e
							.statusBarHeight);
					//#ifdef H5 || APP-PLUS
					this.globalData.customBar = 50 + e.statusBarHeight;
					//#endif

					// #ifdef H5

					let url = window.location.href;
					if (url.indexOf('custom=1') > -1) {
						//苹果加状态栏
						this.globalData.statusBar = 44;
						this.globalData.customBar = 50 + 44;
					}
					// #endif

				}
			});


			// #ifdef APP-PLUS
			setTimeout(() => {
				plus.screen.lockOrientation("portrait-primary");
			}, 10)
			// #endif




			uni.onTabBarMidButtonTap(() => {
				uni.switchTab({
					url: '/pages/user/user',
				})
			});





		},
		onShow: function (e) {
			console.log('App show');
			this.globalData.STOPREFRESH = false;
			uni.getSystemInfo({
				success: e => {
					this.globalData.statusBar = e.statusBarHeight; //状态栏高度
					let custom = {
						width: 0,
						height: 28,
						top: 0,
						left: 0,
						right: 0,
						bottom: 0
					}; //菜单按钮
					//#ifdef MP-WEIXIN || MP-ALIPAY
					custom = uni.getMenuButtonBoundingClientRect(); //菜单按钮
					console.log('custom', custom);
					if (custom.height == 0) {
						custom.height = 40;
						console.log('未获取到custom，手动设置为', custom);
					}
					//#endif

					if (e.safeArea) {
						// #ifdef MP-WEIXIN
						this.globalData.safeArea = e.screenHeight - e.safeArea.bottom
						// #endif
						// #ifndef MP-WEIXIN
						this.globalData.safeArea = e.safeAreaInsets.bottom
						// #endif
					} else {
						this.globalData.safeArea = 0
					}

					this.globalData.screenHeight = e.screenHeight;
					this.globalData.screenWidth = e.screenWidth;
					this.globalData.custom = custom;
					this.globalData.customBar = (custom.bottom + custom.top - e.statusBarHeight) > 0 ?
						(custom.bottom + custom.top - e.statusBarHeight) : (custom.height + e
							.statusBarHeight);
					//#ifdef H5 || APP-PLUS
					this.globalData.customBar = 50 + e.statusBarHeight;
					//#endif

					// #ifdef H5

					let url = window.location.href;
					if (url.indexOf('custom=1') > -1) {
						//苹果加状态栏
						this.globalData.statusBar = 44;
						this.globalData.customBar = 50 + 44;
					}
					// #endif

				}
			});

			///打开app携带的参数
			let objectToUrlParams = (obj, urlencode) => {
				let str = "";
				for (let key in obj) {
					str += "&" + key + "=" + (urlencode ? encodeURIComponent(obj[key]) : obj[key]);
				}
				return str.substr(1);
			}
			//#ifdef APP-PLUS 
			var args1 = plus.runtime.arguments;
			if (args1) {
				let query_str = args1.split('?');
				if (query_str.length == 2) {
					let arr = query_str[1].split('&').map(val => val.split('='));
					let obj = {};
					arr.forEach(val => {
						obj[val[0]] = val[1];
					})
					if (obj.path) {
						let path = obj.path;
						delete obj.path
						uni.reLaunch({
							url: path + '?' + objectToUrlParams(obj),
						})

					} else {
						this.globalData.path = obj.path;
						delete obj.path;
						this.globalData.query = obj;
					}


				}
			}
			//#endif

			if (this.globalData.APPHIDE && this.globalData.CHAT) {
				this.globalData.APPHIDE = false;
				this.globalData.CHAT.SENDLINE_2();
				this.globalData.REFRESHNOTES = true;
			}


			// 更新小程序...........................
			//#ifdef MP-WEIXIN || MP-ALIPAY
			const updateManager = uni.getUpdateManager();

			updateManager.onCheckForUpdate(function (res) {
				// 请求完新版本信息的回调
				if (res.hasUpdate) {
					updateManager.onUpdateReady(function (res) {
						uni.showModal({
							title: '更新提示',
							content: '新版本已经准备好，是否立即体验？',
							success(res) {
								if (res.confirm) {
									updateManager.applyUpdate();
								}
							}
						});

					});

					updateManager.onUpdateFailed(function (res) {
						// 新的版本下载失败
						uni.showModal({
							title: '已经有新版本了哟~',
							content: '新版本已经上线啦~，请您删除当前小程序，重新打开哟~'
						})
					});
				}
			});


			//#endif



			if (this.globalData.subscribeEvent) {
				//第三方支付回调
				uni.$emit(this.globalData.subscribeEvent.key, this.globalData.subscribeEvent.orderId);
				console.log('this.globalData.subscribeEvent', this.globalData.subscribeEvent);
				this.globalData.subscribeEvent = null;
			}

		},
		onHide: function () {
			console.log('App Hide');
			this.globalData.APPHIDE = true;
			this.globalData.STOPREFRESH = true;
		}
	}
</script>
<style>
	/* 每个页面公共css */
	.uni-page-body,
	.html,
	.body,
	page {
		width: 100% !important;
		height: 100% !important;
		overflow: hidden;
	}
</style>

<style lang="scss">
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}

	/* #ifndef APP-NVUE */
	@import "./components/common/common.css";
	@import "./components/common/iconfont.css";
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	@import '@/components/common/nvue.css';
	/* #endif */
</style>